package com.axend.lib_base.adapter

import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.databinding.BindingAdapter
import androidx.lifecycle.LiveData
import com.axend.lib_base.R
import com.axend.lib_base.ext.dp2px
import com.axend.lib_base.ext.margin
import com.axend.lib_base.ext.roundToOneDecimalPlace
import com.axend.lib_base.utils.CommUtils
import com.axend.lib_base.view.CustomSettingItemView
import com.axend.lib_base.view.input.CustomInputView

object CustomViewBindingAdapter {

    @JvmStatic
    @BindingAdapter("showInput")
    fun setShowInput(view: CustomInputView, showInputLiveData: LiveData<Boolean>) {
        showInputLiveData.observeForever { isVisible ->
            if (isVisible) {
                view.updateVisibility(true, false, false, false)
            }
        }
    }


    @JvmStatic
    @BindingAdapter("showSpinner")
    fun setShowSpinner(view: CustomInputView, showSpinnerLiveData: LiveData<Boolean>) {
        showSpinnerLiveData.observeForever { isVisible ->
            if (isVisible) {
                view.updateVisibility(false, true, false, false)
            }
        }
    }


    /**
     * 定义app:showText
     */
    @JvmStatic
    @BindingAdapter("showText")
    fun setShowText(view: CustomInputView, showTextLiveData: LiveData<Boolean>) {
        showTextLiveData.observeForever { isVisible ->
            if (isVisible) {
                view.updateVisibility(false, false, true, false)
            }
        }
    }

    /**
     * 定义app:showSwitch
     */
    @JvmStatic
    @BindingAdapter("showSwitch")
    fun setShowSwitch(view: CustomInputView, showSwitchLiveData: LiveData<Boolean>) {
        showSwitchLiveData.observeForever { isVisible ->
            if (isVisible) {
                view.updateVisibility(false, false, false, true)
            }
        }
    }

    /**
     * 定义float转string带默认值--
     */
    @JvmStatic
    @BindingAdapter("floatToString")
    fun setFloatToString(view: AppCompatTextView, value: Float) {
        view.text = value.roundToOneDecimalPlace()
    }


    /**
     * 定义app:background
     */
    @JvmStatic
    @BindingAdapter("deviceBackground")
    fun setBackground(view: RelativeLayout, isSelectModel: Boolean) {
        if (isSelectModel) {
            view.setBackgroundResource(R.color.transparent)
        } else {
            view.setBackgroundResource(R.mipmap.img_sensor_background)
        }
    }

    @JvmStatic
    @BindingAdapter("textContent")
    fun setTextModelRes(view: TextView, content: String) {
        if (content.isNullOrEmpty()) {
            return
        }
        if (content == "-1") {
            view.text = CommUtils.getContext().getString(R.string.app_info_model_name, "null")
            return
        }
        view.text = CommUtils.getContext().getString(R.string.app_info_model_name, content)
    }


    @JvmStatic
    @BindingAdapter("custom_setting_rightText")
    fun setCustomSettingViewRightText(view: CustomSettingItemView, content: String) {
        view.rightText = content
    }


    @JvmStatic
    @BindingAdapter("setSofaSrc")
    fun setSofaSrc(view: ImageView, isSelectModel: Boolean) {
        if (isSelectModel) {
            view.layoutParams = view.layoutParams.apply {
                width = dp2px(180f)
                height = dp2px(180f)
            }
            view.setImageResource(R.mipmap.img_shafa_click)
        } else {
            view.layoutParams = view.layoutParams.apply {
                width = dp2px(140f)
                height = dp2px(140f)
            }
            view.setImageResource(R.mipmap.img_sofa)
        }
    }

    @JvmStatic
    @BindingAdapter("setTextViewVisibility")
    fun setTextViewVisibility(view: TextView, isVisible: Boolean) {
        view.visibility = if (isVisible) {
            android.view.View.VISIBLE
        } else {
            android.view.View.GONE
        }
    }

    @JvmStatic
    @BindingAdapter("setModelMarginTop")
    fun setModelMarginTop(view: TextView, isSelect: Boolean) {
        view.margin(topMargin = if (isSelect) 40 else 0)
    }
}

